<template>
  <div class="mod-seckill-shopAuditing">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <template slot="search">
        <el-col :md="10" :xs="24">
          <el-form-item
            :label="$t('order.applicationTime')"
            :label-width="this.$i18n.t('language') === 'language' ? '120px' : '80px'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :range-separator="$t('date.tip')"
              value-format="yyyy-MM-dd HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </template>
      <template slot="menuLeft">
        <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="insertShop()"
        >{{$t('crud.addTitle')}}</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-edit"
          size="small"
          type="primary"
          v-if="isAuth('shop:shopAuditing:audit') && scope.row.shopStatus === -1"
          @click="auditHandle(scope.row.shopId)"
        >{{$t('productComm.audit')}}</el-button>
        <el-button
          icon="el-icon-view"
          size="small"
          type="primary"
          v-else-if="isAuth('shop:shopAuditing:info')"
          @click="auditHandle(scope.row.shopId)"
        >{{$t('seckill.view')}}</el-button>
        <el-button
          icon="el-icon-edit"
          size="small"
          type="primary"
          v-if="isAuth('shop:shopAuditing:audit') && scope.row.mobile"
          @click="shopAccountHandle(scope.row.shopId)"
        >{{$t('shop.accountManagement')}}</el-button>
        <el-button
          type="danger"
          icon="el-icon-bottom"
          size="small"
          v-if="isAuth('shop:shopAuditing:lowerShelfShop') && scope.row.shopStatus == 1"
          @click="offlineHandle(scope.row)"
        >{{$t('shop.offlineShop')}}</el-button>
        <el-button
          icon="el-icon-document-checked"
          size="small"
          type="danger"
          v-if="isAuth('shop:shopAuditing:lowerShelfShop') && scope.row.shopStatus > 1"
          @click="shopAuditHandle(scope.row.shopId)"
        >{{$t('distributionProdLog.offlineManagement')}}</el-button>
      </template>
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 0" size="small">{{$t('liveRoom.notReviewed')}}</el-tag>
        <el-tag v-else-if="scope.row.status === 1" size="small" type="success">{{$t('shop.passed')}}</el-tag>
        <el-tag
          v-else-if="scope.row.status === -1"
          size="small"
          type="danger"
        >{{$t('shop.notPass')}}</el-tag>
        <el-tag v-else size="small" type="success">{{$t('shop.platformCreate')}}</el-tag>
      </template>
      <template slot-scope="scope" slot="shopStatusList">
        <el-tag
          v-if="scope.row.shopStatus === -1"
          size="small"
          type="info"
        >{{$t('shop.nonactivated')}}</el-tag>
        <el-tag v-else-if="scope.row.shopStatus === 0" size="small">{{$t('shop.closed')}}</el-tag>
        <el-tag
          v-else-if="scope.row.shopStatus === 1"
          size="small"
          type="success"
        >{{$t('shop.inOperation')}}</el-tag>
        <el-tag
          v-else-if="scope.row.shopStatus === 2"
          size="small"
          type="danger"
        >{{$t('groups.offlineViolation')}}</el-tag>
        <el-tag
          v-else-if="scope.row.shopStatus === 3"
          size="small"
          type="warning"
        >{{$t('product.pendingReview')}}</el-tag>
      </template>
    </avue-crud>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <!-- 商品审核弹窗 -->
    <ShopAudit
      v-if="shopAuditVisible"
      ref="shopAudit"
      selectUrl="/platform/shopDetail/getOfflineHandleEventByShopId"
      updateUrl="/platform/shopDetail/auditShop"
      @refreshDataList="refreshChange"
    ></ShopAudit>
    <!-- 账号管理 -->
    <ShopAccount v-if="shopAccountVisible" ref="shopAccount" @refreshDataList="refreshChange"></ShopAccount>
  </div>
</template>

<script>
import { tableOption } from '@/crud/platform/auditShop'
import AddOrUpdate from './auditShop-add-or-update.vue'
// import ShopAudit from '@/components/offline-audit'
import ShopAudit from './shopOfflineHandle'
import ShopAccount from './shop-account.vue'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {
        shopStatusList: [1, 2]
      }, // 搜索
      dateRange: [],
      dataListSelections: [],
      dataListLoading: false,
      tableOption: tableOption,
      addOrUpdateVisible: false,
      shopAuditVisible: false,
      shopAccountVisible: false
    }
  },
  components: {
    AddOrUpdate,
    ShopAudit,
    ShopAccount
  },
  created () {
  },
  mounted () {
  },
  methods: {
    getDataList (page, params) {
      params = this.searchForm
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/shop/shopAuditing/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          'current': page.currentPage,
          'size': page.pageSize,
          'beginTime': this.dateRange ? this.dateRange[0] : null, // 开始时间
          'endTime': this.dateRange ? this.dateRange[1] : null // 结束时间
        }, params
        ))
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.page.currentPage = data.current
        this.dataListLoading = false

        // let pageCont = document.getElementsByClassName('el-pager')[0].children
        // if (data.current !== 1) {
        //   pageCont[0].classList.remove('active')
        // }
      })
    },
    // 查看 / 审核
    auditHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 账号管理
    shopAccountHandle (id) {
      this.shopAccountVisible = true
      this.$nextTick(() => {
        this.$refs.shopAccount.init(id)
      })
    },
    // 新增 / 修改
    insertShop () {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init()
      })
    },
    // 下架店铺
    offlineHandle (row) {
      // 确定对店铺[' + row.shopName + ']进行下线操作？如是，请填写下线理由
      var tips = this.$i18n.t('shop.shopTip1')
      tips = tips.replace('SHOPNAME', row.shopName)
      this.$prompt(tips, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('coupon.confirm'),
        cancelButtonText: this.$i18n.t('coupon.cancel')
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error(this.$i18n.t('seckill.offlineReasonTip'))
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/shopDetail/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.shopId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    /**
     * 刷新回调
     */
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 清空自定义数据
    searchReset () {
      this.dateRange = []
    },
    searchChange (params) {
      if (!params.shopStatusList) {
        params.shopStatusList = [1, 2]
      }
      this.searchForm = params

      // 修复页数内容不对应问题
      // let pageCont = document.getElementsByClassName('el-pager')[0].children
      // for (var i = 0; i < pageCont.length; i++) {
      //   pageCont[i].classList.remove('active')
      // }
      // pageCont[0].classList.add('active')

      this.page.currentPage = 1
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    // 弹窗处理
    shopAuditHandle (id) {
      this.shopAuditVisible = true
      this.$nextTick(() => {
        this.$refs.shopAudit.init(id)
      })
    }
  }
}
</script>
<style scoped>
div /deep/ .avue-crud__pagination{
  margin-bottom: 25px;
}
</style>
